﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.enprise
{
    public partial class SetTop : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["enterprise_bid"] != null)
                {
                    if (Application["webname"] != null)
                    {
                        this.Title = string.Format("单位管理中心—单位升级：首页固定位置  {0}", Application["webname"].ToString());
                    }
                    int enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);
                    Bind(enterprise_bid);
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登陆！", "EnterpriseLogin.html");
                }
            }
        }
        //固定首页位置
        protected void lbn_save_Click(object sender, EventArgs e)
        {
            int timecount = Convert.ToInt32(this.txt_timecount.Text);

            applytop(timecount);
        }
        //固定首页位置
        protected void lbn_save_Click_add(object sender, EventArgs e)
        {
            //int timecount = Convert.ToInt32(this.txt_addcount.Text);

            //applytop(timecount);
        }

        private void applytop(int timecount)
        {
            decimal price = (100 / 7) * timecount;

            if (Session["enterprise_bid"] != null)
            {
                int enterprise_bid = Convert.ToInt32(Session["enterprise_bid"]);

                lcmyjobs.Model.enterprise_top et = new Model.enterprise_top();

                et.enterprise_bid = enterprise_bid;
                et.price = price;
                et.status = 0;
                et.timecount = timecount;
                et.topapplicationtime = DateTime.Now;


                if (new lcmyjobs.BLL.enterprise_top().Add(et))
                {
                    Maticsoft.Common.MessageBox.Show(this, "申请固定首页位置成功！");

                    Bind(enterprise_bid);
                }

            }

        }

        private void Bind(int enterprise_bid)
        {
            lcmyjobs.BLL.enterprise_top em = new BLL.enterprise_top();

            DataSet ds = em.GetList(enterprise_bid);

            
            this.lbl_title.Text = " " + ds.Tables[1].Rows[0][0].ToString();

            this.lbl_top_apply.Visible = false;
            this.lbl_top_suc.Visible = false;
            this.lbl_top_no.Visible = false;
            this.lbl_top_add.Visible = false;
            this.lbl_btn_1.Visible = false;
            this.lbl_btn_2.Visible = false;
            this.lbn_save.Visible = false;
            this.txt_timecount.Visible = false;
            // add by liuhui 20131120
            if (ds.Tables[0].Rows.Count > 0)
            {
                
                if (ds.Tables[0].Rows[0]["status"].ToString() == "0")
                {
                    this.lbl_top_apply.Visible = true;
                }
                else if(ds.Tables[0].Rows[0]["status"].ToString() == "1")
                {
                    this.lbl_top_suc.Visible = true;
                    this.lbl_btn_1.Visible = true;
                    this.lbl_btn_2.Visible = true;
                    this.lbn_save.Visible = true;
                    this.txt_timecount.Visible = true;
                    if (ds.Tables[2].Rows.Count > 0)
                    {

                        if (ds.Tables[2].Rows[0]["validity_last"].ToString() == "" || Convert.ToInt32(ds.Tables[2].Rows[0]["validity_last"].ToString()) < 0)
                        {
                            this.lbl_top_valid.Text = "已过期";
                        }
                        else
                        {
                            this.lbl_top_valid.Text = "还有"+ds.Tables[2].Rows[0]["validity_last"].ToString() + "天";
                        }
                    }
                }
                else if (ds.Tables[0].Rows[0]["status"].ToString() == "2")
                {
                    this.lbl_top_add.Visible = true;
                }
            }
            else
            {
                this.lbl_top_no.Visible = true;

                this.lbl_btn_1.Visible = true;
                this.lbl_btn_2.Visible = true;
                this.lbn_save.Visible = true;
                this.txt_timecount.Visible = true;
            }

        }
    }
}